/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler;

import com.camachosanchez.bean.Flotillas;
import com.camachosanchez.bean.Vehiculos;
import com.camachosanchez.controler.exceptions.NonexistentEntityException;
import com.camachosanchez.util.JPAUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

/**
 *
 * @author ZULLY
 */
public class FlotillasJpaController implements Serializable {

    public FlotillasJpaController(EntityManagerFactory emf) {
        this.emf = JPAUtil.getEntityManagerFactory();
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Flotillas flotillas) {
        if (flotillas.getVehiculosList() == null) {
            flotillas.setVehiculosList(new ArrayList<Vehiculos>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Vehiculos> attachedVehiculosList = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListVehiculosToAttach : flotillas.getVehiculosList()) {
                vehiculosListVehiculosToAttach = em.getReference(vehiculosListVehiculosToAttach.getClass(), vehiculosListVehiculosToAttach.getId());
                attachedVehiculosList.add(vehiculosListVehiculosToAttach);
            }
            flotillas.setVehiculosList(attachedVehiculosList);
            em.persist(flotillas);
            for (Vehiculos vehiculosListVehiculos : flotillas.getVehiculosList()) {
                Flotillas oldIdFlotillaOfVehiculosListVehiculos = vehiculosListVehiculos.getIdFlotilla();
                vehiculosListVehiculos.setIdFlotilla(flotillas);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
                if (oldIdFlotillaOfVehiculosListVehiculos != null) {
                    oldIdFlotillaOfVehiculosListVehiculos.getVehiculosList().remove(vehiculosListVehiculos);
                    oldIdFlotillaOfVehiculosListVehiculos = em.merge(oldIdFlotillaOfVehiculosListVehiculos);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Flotillas flotillas) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Flotillas persistentFlotillas = em.find(Flotillas.class, flotillas.getId());
            List<Vehiculos> vehiculosListOld = persistentFlotillas.getVehiculosList();
            List<Vehiculos> vehiculosListNew = flotillas.getVehiculosList();
            List<Vehiculos> attachedVehiculosListNew = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListNewVehiculosToAttach : vehiculosListNew) {
                vehiculosListNewVehiculosToAttach = em.getReference(vehiculosListNewVehiculosToAttach.getClass(), vehiculosListNewVehiculosToAttach.getId());
                attachedVehiculosListNew.add(vehiculosListNewVehiculosToAttach);
            }
            vehiculosListNew = attachedVehiculosListNew;
            flotillas.setVehiculosList(vehiculosListNew);
            flotillas = em.merge(flotillas);
            for (Vehiculos vehiculosListOldVehiculos : vehiculosListOld) {
                if (!vehiculosListNew.contains(vehiculosListOldVehiculos)) {
                    vehiculosListOldVehiculos.setIdFlotilla(null);
                    vehiculosListOldVehiculos = em.merge(vehiculosListOldVehiculos);
                }
            }
            for (Vehiculos vehiculosListNewVehiculos : vehiculosListNew) {
                if (!vehiculosListOld.contains(vehiculosListNewVehiculos)) {
                    Flotillas oldIdFlotillaOfVehiculosListNewVehiculos = vehiculosListNewVehiculos.getIdFlotilla();
                    vehiculosListNewVehiculos.setIdFlotilla(flotillas);
                    vehiculosListNewVehiculos = em.merge(vehiculosListNewVehiculos);
                    if (oldIdFlotillaOfVehiculosListNewVehiculos != null && !oldIdFlotillaOfVehiculosListNewVehiculos.equals(flotillas)) {
                        oldIdFlotillaOfVehiculosListNewVehiculos.getVehiculosList().remove(vehiculosListNewVehiculos);
                        oldIdFlotillaOfVehiculosListNewVehiculos = em.merge(oldIdFlotillaOfVehiculosListNewVehiculos);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = flotillas.getId();
                if (findFlotillas(id) == null) {
                    throw new NonexistentEntityException("The flotillas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Flotillas flotillas;
            try {
                flotillas = em.getReference(Flotillas.class, id);
                flotillas.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The flotillas with id " + id + " no longer exists.", enfe);
            }
            List<Vehiculos> vehiculosList = flotillas.getVehiculosList();
            for (Vehiculos vehiculosListVehiculos : vehiculosList) {
                vehiculosListVehiculos.setIdFlotilla(null);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
            }
            em.remove(flotillas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Flotillas> findFlotillasEntities() {
        return findFlotillasEntities(true, -1, -1);
    }

    public List<Flotillas> findFlotillasEntities(int maxResults, int firstResult) {
        return findFlotillasEntities(false, maxResults, firstResult);
    }

    private List<Flotillas> findFlotillasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Flotillas as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Flotillas findFlotillas(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Flotillas.class, id);
        } finally {
            em.close();
        }
    }

    public int getFlotillasCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Flotillas as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
